#!/usr/bin/env python3
"""
测试不同片段数量的拆分
"""

import asyncio
from pathlib import Path
from app.services.video_splitter import VideoSplitter

async def test_progressive_split():
    """渐进式测试片段拆分"""
    
    video_path = Path("uploads/90e15f07-59de-4250-b611-f1cbc6940acc.mp4")
    
    if not video_path.exists():
        print(f"❌ 视频文件不存在: {video_path}")
        return
    
    print(f"🧪 渐进式测试片段拆分")
    print(f"📹 视频文件: {video_path}")
    
    splitter = VideoSplitter()
    
    # 测试不同的片段数量
    test_counts = [3, 5, 10, 15, 20, 25, 28]
    
    for max_segments in test_counts:
        print(f"\n🔍 测试最大片段数: {max_segments}")
        
        try:
            # 清理之前的片段
            video_id = video_path.stem
            segment_dir = Path("uploads/segments") / video_id
            if segment_dir.exists():
                import shutil
                shutil.rmtree(segment_dir)
            
            result = await splitter.split_video_by_duration(
                video_path, 
                segment_duration=60,
                max_segments=max_segments,
                min_segment_duration=10
            )
            
            segments_count = len(result.get('segments', []))
            success = segments_count > 0
            
            print(f"   结果: {'✅ 成功' if success else '❌ 失败'}")
            print(f"   生成片段数: {segments_count}")
            
            if success:
                # 检查文件是否真实存在
                actual_files = 0
                total_size = 0
                for segment in result['segments']:
                    segment_path = Path(segment['path'])
                    if segment_path.exists():
                        actual_files += 1
                        total_size += segment_path.stat().st_size
                
                print(f"   实际文件数: {actual_files}")
                print(f"   总文件大小: {total_size / (1024*1024):.1f}MB")
                
                if actual_files != segments_count:
                    print(f"   ⚠️ 警告: 生成的片段数与实际文件数不匹配")
            else:
                print(f"   ❌ 在 {max_segments} 个片段时失败")
                break
                
        except Exception as e:
            print(f"   ❌ 测试失败: {e}")
            break

if __name__ == "__main__":
    asyncio.run(test_progressive_split())